package com.ttc.group2.videochat.utils;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.quickblox.auth.QBAuth;
import com.quickblox.auth.model.QBSession;
import com.quickblox.core.QBEntityCallbackImpl;
import com.quickblox.core.QBSettings;
import com.quickblox.users.QBUsers;
import com.quickblox.users.model.QBUser;

public class ApiUtil {

	protected static final String TAG = "ApiUtil";

	public static List<QBUser> getDataFromServer(final Context context) {
		final List<QBUser> listUsers = new ArrayList<QBUser>();

		// Set application credentials
		QBSettings.getInstance().fastConfigInit(Const.APP_ID, Const.AUTH_KEY,
				Const.AUTH_SECRET);

		// Get all users
		QBAuth.createSession(new QBEntityCallbackImpl<QBSession>() {

			@Override
			public void onError(List<String> errors) {
				super.onError(errors);
				DialogUtils.showToast(context, errors.get(0),
						Toast.LENGTH_SHORT);
			}

			@Override
			public void onSuccess(QBSession result, Bundle params) {
				super.onSuccess(result, params);
				listUsers.addAll(getAllUsers(context));

				for (QBUser user : listUsers) {
					Log.d(TAG, "name: " + user.getFullName() + " email: "
							+ user.getEmail());
				}
			}

		});

		return listUsers;
	}

	private static List<QBUser> getAllUsers(final Context context) {
		final List<QBUser> listUsers = new ArrayList<QBUser>();
		QBUsers.getUsers(null, new QBEntityCallbackImpl<ArrayList<QBUser>>() {

			@Override
			public void onError(List<String> errors) {
				super.onError(errors);
				DialogUtils.showToast(context, errors.get(0),
						Toast.LENGTH_SHORT);
			}

			@Override
			public void onSuccess(ArrayList<QBUser> qbUsers, Bundle bundle) {
				super.onSuccess(qbUsers, bundle);
				listUsers.addAll(qbUsers);
			}

		});
		return listUsers;
	}
}
